package com.lq.demo.demo.mode.websocket;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;


/**
 * websocket 配置类
 */
@Configuration
@EnableWebSocket
public class MessageWebSocketConfig implements WebSocketConfigurer {
    //实例化
    @Autowired
    private MessageWebSocketInterceptor messageWebSocketInterceptor;
    @Autowired
    private MessageWebSocketHandler messageWebSocketHandler;

    //注册相关
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        ///demo/webSocket 对应前端的js里的websocket（客户端）url  setAllowedOrigins("*") 允许跨域
        registry.addHandler(messageWebSocketHandler, "/demo/webSocket").setAllowedOrigins("*")
                .addInterceptors(messageWebSocketInterceptor); //添加拦截器
    }

}